#!/usr/bin/env luajit

local name = "hello_world"
local title = "Hello World"
local description = "Example mod."
local min_minetest_version = 5.6
local depends = "default"
local optional_depends = ""
local author = "luarocks"

local readme = ([[# %s

%s

---

This mod was created in [Rustroot Forge].

All my mods uses [Luanti LSP-API] annotations, [StyLua] and [Luacheck].

[Rustroot Forge]: https://notabug.org/rr
[Luanti LSP-API]: https://notabug.org/rr/luanti_lsp_api
[StyLua]: https://github.com/JohnnyMorganz/StyLua
[Luacheck]: https://github.com/mpeterv/luacheck
]]):format(title, description)

local mod_conf = ([[name = %s
title = %s
description = %s
min_minetest_version = %s

# Optional
depends = %s
optional_depends = %s
author = %s]]):format(
  name,
  title,
  description,
  min_minetest_version,
  depends,
  optional_depends,
  author
)

local template = ([[# textdomain: %s

%s=
%s=
]]):format(name, title, description)

local file = io.open("README.md", "w")
if not file then error "failed to open mod.conf for writing" end
file:write(readme)
file:close()

file = io.open("mod.conf", "w")
if not file then error "failed to open mod.conf for writing" end
file:write(mod_conf)
file:close()

if not os.execute "mkdir locale" then error "failed to create directory" end

file = io.open("locale/template.txt", "w")
if not file then error "failed to create file locale/template.txt" end
file:write(template)
file:close()

local path = ("locale/%s.ru.tr"):format(name)
file = io.open(path, "w")
if not file then error(("failed to create file " .. path)) end
file:write(template)
file:close()

local function remove_dir(dir_path)
  local os_name = package.config:sub(1, 1)
  if os_name == "/" then
    os.execute('rm -rf "' .. dir_path .. '"')
  else
    os.execute('rmdir /S /Q "' .. dir_path .. '"')
  end
end

remove_dir ".git"
os.remove "setup.lua"

os.execute "git init"
os.execute "git add ."
os.execute 'git commit -m "Init"'
